using UnityEngine;

namespace WorkFlow.Games._1_GameEntrance.Scripts
{
    [CreateAssetMenu(fileName = "ToolBaseConfig",menuName = "Data/ToolBaseConfig")]
    public class ToolBaseConfig : ScriptableObject
    {
        [SerializeField] private string toolsVersion;
        
#if UNITY_EDITOR
        [SerializeField] private string buildVersion;
        public static string BuildVersion
        {
            get
            {
                if (_instance == null)
                {
                    _instance =  Resources.Load<ToolBaseConfig>("ToolBaseConfig");
                }
        
                return _instance.buildVersion;
            }
        }
#endif

        static ToolBaseConfig _instance;
        public static string Version
        {
            get
            {
                if (_instance == null)
                {
                    _instance =  Resources.Load<ToolBaseConfig>("ToolBaseConfig");
                }
        
                return _instance.toolsVersion;
            }
        }

        /// <summary>
        /// 比较两个版本号字符串
        /// </summary>
        /// <param name="v1">版本号1，例如 "1.2.3"</param>
        /// <param name="v2">版本号2，例如 "1.2.10"</param>
        /// <returns>
        /// -1 表示 v1 小于v2 , 0 表示 v1 == v2  ,1 表示 v1 大于 v2  
        /// </returns>
        public static int Compare(string v1, string v2)
        {
            if (string.IsNullOrEmpty(v1) || string.IsNullOrEmpty(v2))
                return 0;

            var parts1 = v1.Split('.');
            var parts2 = v2.Split('.');

            int length = Mathf.Max(parts1.Length, parts2.Length);

            for (int i = 0; i < length; i++)
            {
                int n1 = (i < parts1.Length && int.TryParse(parts1[i], out int t1)) ? t1 : 0;
                int n2 = (i < parts2.Length && int.TryParse(parts2[i], out int t2)) ? t2 : 0;

                if (n1 < n2) return -1;
                if (n1 > n2) return 1;
            }

            return 0;
        }
    }
}
